﻿Imports System.ServiceModel.DomainServices.Client

Partial Public Class FullDeCaixaDespesesIngresos
    Inherits ChildWindow

    Private _DespesaIngres As Web.LLibreCaixaDespesaIngres
    Public ReadOnly Property DespesaIngres As Web.LLibreCaixaDespesaIngres
        Get
            Return _DespesaIngres
        End Get
    End Property

    Private _Mode As EnumMode
    Public ReadOnly Property Mode As EnumMode
        Get
            Return _Mode
        End Get
    End Property

    Public Enum EnumMode
        ModeAlta
        ModeModificacio
    End Enum

#Region "Crear i Destruir"
    Public Sub New(pMode As EnumMode, pDespesaIngres As Web.LLibreCaixaDespesaIngres)
        MyBase.New()

        InitializeComponent()
        _Mode = pMode
        _DespesaIngres = pDespesaIngres


        Me.cboTipus.ItemsSource = OpcionsComboTipusDespesaIngres.GetOpcions
    End Sub

    Private Sub Child_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
        Application.Current.RootVisual.SetValue(Control.IsEnabledProperty, True)
    End Sub

    Private Sub FullDeCaixa_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If RiaContext.Current.Authentication.User.Identity.IsAuthenticated = True Then
            Me.Activity.IsActive = True

            AddHandler Contexte.TipusDespesaIngresCarregats, AddressOf TipusDespesaIngresCarregats
            Contexte.LoadTipusDespesaIngres()

            If Mode = EnumMode.ModeAlta Then
                Me.Title = StringLibrary.AltaDespesaIngres
                _DespesaIngres.IdTipusDespesaIngres = -1
                _DespesaIngres.Import = 0

                Me.cboTipusDespesaIngres.IsEnabled = False
                Me.txtImport.IsEnabled = False
                Me.txtObservacions.IsEnabled = False

            ElseIf Mode = EnumMode.ModeModificacio Then
                Me.Title = StringLibrary.ModificarDespesaIngres

                Me.cboTipus.IsEnabled = False
                If _DespesaIngres.EsDespesa Then
                    Me.cboTipus.SelectedItem = (From OC As OpcioCombo In OpcionsComboTipusDespesaIngres.GetOpcions Where OC.Id = EnumTipusDespesaIngres.Despesa).FirstOrDefault
                Else
                    Me.cboTipus.SelectedItem = (From OC As OpcioCombo In OpcionsComboTipusDespesaIngres.GetOpcions Where OC.Id = EnumTipusDespesaIngres.Ingres).FirstOrDefault
                End If
            End If

            Me.gridDetall.DataContext = _DespesaIngres

        End If
    End Sub
#End Region

#Region "Tipus"
    Private Sub cboTipus_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs)
        If e.AddedItems.Count > 0 Then
            Dim Dades As List(Of Web.TipusDespesaIngres)

            If DirectCast(e.AddedItems(0), OpcioCombo).Id = EnumTipusDespesaIngres.Despesa Then
                Dades = (From Tipus As Web.TipusDespesaIngres In Contexte.TipusDespesaIngres Where Tipus.EsGasto = True And Tipus.EsBaixa = False Order By Tipus.Nom).ToList
                Me._DespesaIngres.EsDespesa = True
            Else
                Dades = (From Tipus As Web.TipusDespesaIngres In Contexte.TipusDespesaIngres Where Tipus.EsGasto = False And Tipus.EsBaixa = False Order By Tipus.Nom).ToList
                Me._DespesaIngres.EsDespesa = False
            End If

            'Per si en modifiquem un que està de baixa
            If Not Dades.Contains(_DespesaIngres.TipusDespesaIngres) Then
                Dades.Add(_DespesaIngres.TipusDespesaIngres)
            End If

            cboTipusDespesaIngres.ItemsSource = Dades

            Me.cboTipusDespesaIngres.IsEnabled = True
            Me.txtImport.IsEnabled = True
            Me.txtObservacions.IsEnabled = True
            Me.cboTipus.IsEnabled = False
        Else
            Me.cboTipusDespesaIngres.IsEnabled = False
            Me.txtImport.IsEnabled = False
            Me.txtObservacions.IsEnabled = False
            Me.cboTipus.IsEnabled = True
        End If

    End Sub
#End Region

#Region "Carregar Dades"

    Private Sub TipusDespesaIngresCarregats(ByVal Resultat As Boolean, ByVal Missatge As String)
        RemoveHandler Contexte.TipusDespesaIngresCarregats, AddressOf TipusDespesaIngresCarregats


        Me.Activity.IsActive = False
    End Sub

#End Region

#Region "Guardar i Cancelar"
    Private Sub OKButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        EliminarObjectErrors()

        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            If ValidarDades() And Validacio.Errors.Count = 0 Then
                LullabyFullDeCaixa.Contexte.DomainContext.SubmitChanges(AddressOf DomainContext_SubmittedChanges, Nothing)
                Activity.IsActive = True
            End If
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Function ValidarDades() As Boolean
        Dim Resultat As Boolean = True

        If _DespesaIngres.IdTipusDespesaIngres = -1 Then
            Me.Validacio.Errors.Add(New ValidationSummaryItem(StringLibrary.ConcepteNoValid))
            Resultat = False
        End If

        Return Resultat
    End Function

    Private Sub DomainContext_SubmittedChanges(ByVal s As SubmitOperation)
        Activity.IsActive = False

        If s.HasError Then
            For Each E As Entity In s.EntitiesInError
                For Each VError As ComponentModel.DataAnnotations.ValidationResult In E.ValidationErrors
                    Me.Validacio.Errors.Add(New ValidationSummaryItem(VError.ErrorMessage))
                Next
            Next
            s.MarkErrorAsHandled()
            'App.ReportError(StringLibrary.ErrorGuardantDades)
        Else
            Me.DialogResult = True
        End If
    End Sub

    Private Sub CancelButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        If LullabyFullDeCaixa.Contexte.DomainContext.EntityContainer.HasChanges Then
            LullabyFullDeCaixa.Contexte.DomainContext.RejectChanges()
        End If

        Me.DialogResult = False
    End Sub

    Public Sub EliminarObjectErrors()
        Dim i As Long

        i = 0
        While i < Validacio.Errors.Count
            If Validacio.Errors(i).ItemType = ValidationSummaryItemType.ObjectError Then
                Validacio.Errors.RemoveAt(i)
            Else
                i = i + 1
            End If
        End While
    End Sub
#End Region


End Class
